package org.example.erat.log.controller;

import org.example.erat.log.service.LogViewService;
import org.example.erat.log.service.OperationLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日志控制器
 * 提供日志查看接口
 */
@RestController
@RequestMapping("/logs")
public class LogController {
    
    private final LogViewService logViewService;
    private final OperationLogger operationLogger;
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    @Autowired
    public LogController(LogViewService logViewService, OperationLogger operationLogger) {
        this.logViewService = logViewService;
        this.operationLogger = operationLogger;
    }
    
    /**
     * 获取操作日志
     * @param lines 最大行数，默认100行
     * @return 操作日志内容
     */
    @GetMapping("/operation")
    public ResponseEntity<Map<String, Object>> getOperationLog(
            @RequestParam(value = "lines", defaultValue = "100") int lines) {
        
        List<String> logContent = logViewService.getOperationLog(lines);
        long fileSize = logViewService.getOperationLogSize();
        long lastModified = logViewService.getLogFileLastModified("logs/operation.log");
        
        Map<String, Object> response = new HashMap<>();
        response.put("content", logContent);
        response.put("fileSize", formatFileSize(fileSize));
        response.put("lastModified", dateFormat.format(new Date(lastModified)));
        response.put("lines", logContent.size());
        
        operationLogger.logOperation("查看日志", "查看操作日志，行数: " + lines);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 获取系统日志
     * @param lines 最大行数，默认100行
     * @return 系统日志内容
     */
    @GetMapping("/system")
    public ResponseEntity<Map<String, Object>> getSystemLog(
            @RequestParam(value = "lines", defaultValue = "100") int lines) {
        
        List<String> logContent = logViewService.getSystemLog(lines);
        long fileSize = logViewService.getSystemLogSize();
        long lastModified = logViewService.getLogFileLastModified("logs/system.log");
        
        Map<String, Object> response = new HashMap<>();
        response.put("content", logContent);
        response.put("fileSize", formatFileSize(fileSize));
        response.put("lastModified", dateFormat.format(new Date(lastModified)));
        response.put("lines", logContent.size());
        
        operationLogger.logOperation("查看日志", "查看系统日志，行数: " + lines);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 获取错误日志
     * @param lines 最大行数，默认100行
     * @return 错误日志内容
     */
    @GetMapping("/error")
    public ResponseEntity<Map<String, Object>> getErrorLog(
            @RequestParam(value = "lines", defaultValue = "100") int lines) {
        
        List<String> logContent = logViewService.getErrorLog(lines);
        long fileSize = logViewService.getErrorLogSize();
        long lastModified = logViewService.getLogFileLastModified("logs/error.log");
        
        Map<String, Object> response = new HashMap<>();
        response.put("content", logContent);
        response.put("fileSize", formatFileSize(fileSize));
        response.put("lastModified", dateFormat.format(new Date(lastModified)));
        response.put("lines", logContent.size());
        
        operationLogger.logOperation("查看日志", "查看错误日志，行数: " + lines);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 获取所有日志文件列表
     * @return 日志文件列表
     */
    @GetMapping("/files")
    public ResponseEntity<List<String>> getLogFiles() {
        List<String> logFiles = logViewService.getLogFiles();
        operationLogger.logOperation("查看日志", "获取日志文件列表");
        return ResponseEntity.ok(logFiles);
    }
    
    /**
     * 格式化文件大小
     * @param size 文件大小（字节）
     * @return 格式化后的文件大小
     */
    private String formatFileSize(long size) {
        if (size < 1024) {
            return size + " B";
        } else if (size < 1024 * 1024) {
            return String.format("%.2f KB", size / 1024.0);
        } else if (size < 1024 * 1024 * 1024) {
            return String.format("%.2f MB", size / (1024.0 * 1024));
        } else {
            return String.format("%.2f GB", size / (1024.0 * 1024 * 1024));
        }
    }
}
